<template>
  <forms ref="forms" :config="config" @submit:forms="onsubmit" :btns="btns">
  </forms>
</template>

<script>
import {use_menu_list} from "@/store/menu_list.js";

export default {
  props: ['__iframe'],
  data(){
    let id = this.$get('id'), btns = undefined;
    let forms_config = {
      name: {label:'模型名称',name:'name',type:'text_one',tips:'对应表名，只能为小写字母、数字、下划线'},
      alias: {label:'模型别名',name:'alias',type:'text_one',tips:''},
      info: {label:'介绍',name:'info',type:'text_more',tips:''},
      link_model: {label:'父级模型',name:'link_model',type:'select_more',config:[],tips:'父级模型表名'},
      link_order: {label:'订单系统',name:'link_order',type:'radio_switch',tips:'是否关联订单系统，关联订单系统会创建{name}_order表',value:0},
      link_user: {label:'用户系统',name:'link_user',type:'radio_switch',tips:'是否关联用户系统，关联用户系统会创建{name}_user表',value:0},
      need_sku: {label:'规格系统',name:'need_sku',type:'radio_switch',tips:'是否可以设定规格，开启【订单系统】才有效',value:0},
      need_additional: {label:'定制系统',name:'need_additional',type:'radio_switch',tips:'是否可以设定定制项，开启【订单系统】才有效',value:0},
      action_alias_json: {label:'操作别名',name:'action_alias_json',type:'text_more',tips:'json格式:{"deliver":"发货"}，操作名：发货deliver，收货take，核销use'},
    }
    if (id){
      forms_config.create_time = {label:'创建时间',name:'create_time',tips:''};
      forms_config.name.is_info =
          forms_config.link_model.is_info =
              forms_config.link_order.is_info =
                  forms_config.link_user.is_info = 1;
    }
    if (this.$get('is_info')){
      s94Mjs.each(forms_config, function (row){ row.is_info = true; });
      btns = [];
    }
    return {
      id: id,
      btns: btns,
      config: forms_config,
    }
  },
  methods: {
    onsubmit(formData){
      let url = this.id ? `/admin/model/edit?id=`+this.id : '/admin/model/add';
      ajax({url:url, method:'POST', data:formData}, (res)=>{
        if (this.__iframe){
          iframe.exe('index', '$refs.list_url.list');
          use_menu_list().refresh();
          iframe.hide('edit');
          if (!this.id) this.$refs['forms'].init();
        }else {
          this.$router.push('index');
        }
      }, (res)=>{
        if (res.errors) this.$refs['forms'].errors = res.errors;
      })
    }
  },
  mounted() {
    let app = this;
    if (!this.__iframe) window['app'] = this;
    if (this.id) {
      ajax({url:'/admin/model/info?id='+this.id, cache:true}, (res)=>{
        this.$refs['forms'].init(res.data);
        return false;
      }, ()=>{
        if (this.__iframe){
          iframe.remove('edit');
        }else {
          this.$router.push('index');
        }
      })
    }

    //其他初始化操作
    ajax({url:'/admin/model/list',cache:true},function(res){
      app.config.link_model.config = app.decode_config(res.data.list, {content_key:'alias', value_key:'name'});
      return false
    })
  }
}
</script>
